package com.danco.training.zaneuskaya.repository.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.Account;
import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Gender;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.UserDao;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class UserDaoImpl extends AbstractHibernateDao<User, Integer> implements UserDao {

	

	private static final String USER_CITY = "userCity";
	private static final String USER_COUNTRY = "userCountry";
	private static final String MARITAL_STATE = "maritalState";
	private static final String BIRTHDAY_DATE = "birthdayDate";
	private static final String GENDER_USER = "gender";
	private static final String LAST_NAME = "lastName";
	private static final String FIRST_NAME = "firstName";
	private static final String ACCOUNT_USER = "account";
	private static final String ONLINE = "online";

	@Override
	public User read(Integer PK) throws HibernateException {
		return super.read(User.class, PK);
	}

	@Override
	public List<User> readAll() throws HibernateException {
		
		return super.readAll(User.class, LAST_NAME);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<User> findOnlineUsers()  throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(ONLINE, true));
		
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@Override
	public User findUserByAccount(Account account) throws HibernateException{
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(ACCOUNT_USER, account));
		return (User) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByName(String firstName, String lastName) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.like(FIRST_NAME, firstName));
		criteria.add(Restrictions.like(LAST_NAME, lastName));
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByGender(Gender gender) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(GENDER_USER, gender));
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByBirthday(Date birthday) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(BIRTHDAY_DATE, birthday));
	
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUserByMaritalState(String state) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.like(MARITAL_STATE, state));
		
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByCityCountry(Country country, City city) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(USER_COUNTRY, country));
		criteria.add(Restrictions.eq(USER_CITY, city));
		
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}


	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findOfflineUsers() throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(ONLINE, false));
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUsersByNameGenderBirthdayMaritalStateLocation(
			String firstName, String lastName, Gender gender, Date birthday,
			String state, Country country, City city) throws HibernateException{
		List <User> users = new ArrayList<User>();
		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(USER_COUNTRY, country));
		criteria.add(Restrictions.eq(USER_CITY, city));
		criteria.add(Restrictions.like(FIRST_NAME, firstName));
		criteria.add(Restrictions.like(LAST_NAME, lastName));
		criteria.add(Restrictions.eq(GENDER_USER, gender));
		criteria.add(Restrictions.eq(BIRTHDAY_DATE, birthday));
		criteria.add(Restrictions.like(MARITAL_STATE, state));
		users =  modifyCriteria(criteria,LAST_NAME ).list();
		return users;		
	}

	
	
}
